package com.xmut.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xmut.mall.product.dao.BrandDao;
import com.xmut.mall.product.dao.CategoryDao;
import com.xmut.mall.product.entity.BrandEntity;
import com.xmut.mall.product.entity.CategoryEntity;
import com.xmut.mall.product.service.BrandService;
import com.xmut.mall.product.utils.RespBean;
import com.xmut.mall.product.vo.CategoryBrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.product.utils.PageUtils;
import com.xmut.mall.product.utils.Query;

import com.xmut.mall.product.dao.CategoryBrandRelationDao;
import com.xmut.mall.product.entity.CategoryBrandRelationEntity;
import com.xmut.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao relationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        // 查询品牌的名字和分类的名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);

        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id", catId));
        List<BrandEntity> collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity brandEntity = brandService.getById(brandId);
            return brandEntity;
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 根据分类Id获取品牌和分类的关联数据
     * @param cateId
     * @return
     */
    @Override
    public List<CategoryBrandVO> getCateBrandRelation(Long cateId) {
        List<CategoryBrandRelationEntity> list = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", cateId));
        // 转换为Vo
        List<CategoryBrandVO> vos = list.stream().map(item -> {
            CategoryBrandVO vo = new CategoryBrandVO();
            vo.setId(item.getBrandId());
            vo.setName(item.getBrandName());
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

    /**
     * 添加品牌和分类的关联
     * @param params
     * @return
     */
    @Override
    public RespBean addCateBrandRelation(Map<String, Long> params) {
        // 获取参数
        Long brandId = params.get("brandId");
        Long cateId = params.get("cateId");

        if (brandId!=null && cateId!=null){
            // 先查一遍看看有是不是已经添加过了
            int count = this.count(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId).eq("catelog_id", cateId));
            if (count==0){
                CategoryEntity category = categoryDao.selectById(cateId);
                BrandEntity brand = brandDao.selectById(brandId);
                CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
                categoryBrandRelation.setBrandId(brandId);
                categoryBrandRelation.setCatelogId(cateId);
                categoryBrandRelation.setBrandName(brand.getName());
                categoryBrandRelation.setCatelogName(category.getName());
                // 保存
                boolean save = this.save(categoryBrandRelation);
                if(save){
                    return RespBean.success("保存成功");
                }else {
                    return RespBean.error("保存失败");
                }
            }else {
                // 已经关联过了不需要继续关联
                return RespBean.error("该品牌已进行关联，请勿重复操作");
            }
        }else {
            return RespBean.error("添加失败");
        }

    }

}